﻿<cfmodule template="../../tag/layout.cfm" bodyIsEasyUILayout="false">
<cfscript>
	if( structKeyExists(rc, "id")  && rc.id > 0 ){
		id = rc.id;
	}else{
		id = 0;
	}
</cfscript>
<form method="post" class="form" name="form">
  <fieldset>
    <legend>基本信息</legend>
    <table class="table" width="100%">
      <tbody>
        <tr>
          <th>编号</th>
          <td><input name="id" readonly/></td>
        </tr>
        <tr>
          <th>配送标题</th>
          <td><input name="subject" class="easyui-validatebox" data-options="required:true"  style="width:500px;"/></td>
        </tr>
        <tr>
          <th>选择地区</th>
          <td>
          	<input type="hidden" name="selectedids" id="selectedids">
          	<input type="hidden" name="area" id="area">
          	<ul id="tt" class="easyui-tree" data-options="url:'?action=category.listtree&categorytypeid=100',checkbox:true"></ul></td>
        </tr>
        <tr>
          <th>配送方式</th>
          <td><div id="expresstype"></div></td>
        </tr>
        <tr>
          <th>付款类型</th>
          <td><div id="paytype"></div></td>
        </tr>
        <tr>
          <th>配送规则</th>
          <td>金额小于
          	<input name="ruleexpress" class="easyui-numberbox" data-options="required:true,precision:2,groupSeparator:','" style="width:80px;"/>
           	费用为
           <input id="money1" name="money1" class="easyui-numberbox" data-options="required:true,precision:2,groupSeparator:','" style="width:80px;"/>
           	否则
           <input id="money2" name="money2" class="easyui-numberbox" data-options="required:true,precision:2,groupSeparator:','" style="width:80px;"/>
           </td>
        </tr>
        <tr>
          <th>配送描述</th>
          <td><input name="content" style="width:500px;"/></td>
        </tr>
      </tbody>
    </table>
  </fieldset>
</form>

</cfmodule>

<script type="text/javascript">
	var closeDialog = function($dialog) {
		$dialog.dialog('destroy');
	}

	var submitNow = function($dialog, $grid, $pjq) {
		var url;
		if ($(':input[name="id"]').val().length > 0) {
			url = '?action=logistics.update'; //更新操作
		} else {
			url = '?action=logistics.save';	//新增操作
		}

		console.log( fw.serializeObject($('form')) );
		$.post(url, fw.serializeObject($('form')), function(result) {
			if (result) {
				$grid.datagrid('reload');
				$dialog.dialog('destroy');
			}
		}, 'json');
	};

	var submitForm = function($dialog, $grid, $pjq) {
		if ($('form').form('validate')) {
			submitNow($dialog, $grid, $pjq);
		}
	};

	//加载数据，初始进度条
	parent.$.messager.progress({
		text : '数据加载中....'
	});

	$('#tt').tree({
		onCheck: function(node){
			$('#area').val( getChecked().toString());
		},
		onExpand:function(node){
			checkChecked($('#selectedids').val());
		}
	});

	function getChecked(){
        var nodes = $('#tt').tree('getChecked');
        var ids = [];
        for(var i=0; i<nodes.length; i++){
        	ids.push(nodes[i].id);
        }
        return ids;
    }

	function checkChecked(ids){
		var selectedids = ids.split(',');
		for(var i=0;i<selectedids.length;i++){
			 var node = $('#tt').tree('find',selectedids[i]);
			 $('#tt').tree('check',node.target)

        }
	}

    //配送方式
	$.ajax({
	    type: "POST",
	    url: "?action=category.getall&categorytypeid=7"
	}).done(function(result) {
		var obj = $('#expresstype');
		var html='';
		$(result).each(function(i,e){
			html+='<input name="expresstypeid" class="easyui-validatebox" validType="radio[\'form\',\'expresstypeid\']" type="radio" value="'+ e.id +'"/>' +e.text;
		});
		obj.html( html );
		$.parser.parse(obj);
	}, 'json');

	//支付方式
	$.ajax({
	    type: "POST",
	    url: "?action=category.getall&categorytypeid=8"
	}).done(function(result) {
		var obj = $('#paytype');
		var html='';
		$(result).each(function(i,e){
			html+='<input name="paytypeid" class="easyui-validatebox" validType="radio[\'form\',\'paytypeid\']" type="radio" value="'+ e.id +'"/>' +e.text;
		});
		obj.html( html );

		$.parser.parse(obj);

	}, 'json');

	$(function(){

		var cobj = setInterval(checkObj, 100);
		function checkObj()
		{
			<cfif id gt 0>
				if( $(':input[name="expresstypeid"]').length>0 && $(':input[name="paytypeid"]').length>0){
		            clearInterval(cobj);
		            $.ajax({
					    type: "POST",
					    url: "?action=logistics.get",
					    data: {
					        id:<cfoutput>#id#</cfoutput>
					    }
					}).done(function(result) {
						$('form').form('load', result);
						$('#money1').numberbox('setValue', result.resultexpress.split(',')[0]);
						$('#money2').numberbox('setValue', result.resultexpress.split(',')[1]);

						$.ajax({
						    type: "POST",
						    url: "?action=logisticsarea.getall",
						    data: {
						        logisticsid:<cfoutput>#id#</cfoutput>
						    }
						}).done(function(r) {
							$('#selectedids').val(r.ids);
							checkChecked(r.ids);
						}, 'json');


						parent.$.messager.progress('close');
					}, 'json');
		  		}
		  	<cfelse>
		  		parent.$.messager.progress('close');
			</cfif>
		}

	})
</script>
